<template>
  <div class="dip-login">
    <div class="login-box">
      <el-form
        :model="loginForm"
        :rules="loginRules"
        ref="loginForm"
        label-position="top"
        label-width="100px"
        @keydown.enter.native="onSubmit"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item prop="remember">
          <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')" class="login-button">登录</el-button>
          <el-button type="success" @click="clickRegister" class="register-button">注册</el-button>
        </el-form-item>
        <div class="login-error">{{ errorMsg }}</div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        remember: true
      },
      loginRules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      },
      errorMsg: ''
    }
  },
  created() {
    let rememberJson = window.localStorage.getItem('remember')
    if (rememberJson) {
      let rememberData = JSON.parse(rememberJson)
      this.loginForm.username = rememberData.loginUname
      this.loginForm.password = rememberData.loginPwd
    }
  },
  methods: {
    submitForm() {
      this.errorMsg = ''
      this.$refs['loginForm'].validate((valid) => {
        if (valid) {
          this.$ajax({
            url: this.$ajax.setUrl('auth/login'),
            methods: 'post',
            data: this.$ajax.setData(this.getParams())
          }).then(({ data }) => {
            if (data && data.code === '0') {
              window.localStorage.setItem('token', data.data)
              if (this.loginForm.remember) {
                window.localStorage.setItem('remenber', JSON.stringify(this.getParams()))
              } else {
                window.localStorage.removeItem('remenber')
              }
              // 这里使用replace就是这登录页就不会回溯了，与push的区别就是不会留下历史
              this.$router.replace({ name: 'main' })
            } else {
              this.errorMsg = data.data || data.msg
            }
          })
        }
      })
    },
    getParams() {
      let params = {
        loginUname: this.loginForm.username,
        loginPwd: this.loginForm.password
      }
      return params
    },
    clickRegister() {
      this.$router.push({ name: 'register' })
    }
  }
}
</script>

<style lang="scss">
.dip-login {
  position: relative;
  .login-box {
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -240px;
    width: 480px;
    border-radius: 4px;
    padding: 50px 66px 50px;
    background: #fff;
    border: 1px solid black;
  }
}
</style>
